#!/bin/sh
# postinst script for kobuki-ftdi package
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

# source debconf library
. /usr/share/debconf/confmodule

OLDUDEV="/etc/udev/rules.d/57-kobuki.rules"

# Are we running in a Docker Environment?
docker_cnt=$(awk '$2 == "(1,"' /proc/1/sched  | wc -l)
# docker_cnt == 0 we are in a docker environment

case "$1" in

  configure)
    # Set FORCE_UDEV_IN_DOCKER to non-empty to override
    if [ ${docker_cnt} -eq 0 -a -z "${FORCE_UDEV_IN_DOCKER}" ]
    then
      echo "INFO: Docker environment not supported for udevadm; skipping."
    else
      # Enforce the newly installed  udev rules
      (udevadm control --reload-rules && udevadm trigger) || true
    fi

    # Warn user about /usr/local installation
    if [ -f ${OLDUDEV} ]
    then
      echo "WARNING: Locally installed kobuki_ftdi udev rule should removed:"
      echo "         sudo rm -f ${OLDUDEV}"
    fi
    ;;

  abort-upgrade|abort-remove|abort-deconfigure)
    exit 0
    ;;

  *)
    echo "postinst called with unknown argument \`$1'" >&2
    exit 1
    ;;

esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

db_stop

exit 0
